package com.rui1024.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rui1024.dao.CurdDao;
import com.rui1024.dao.impl.CurdDaoImpl;
import com.rui1024.pojo.Curd;
import com.rui1024.pojo.Menu;
import com.rui1024.util.JDBCUtil;
import com.rui1024.util.PageUtil;
import com.rui1024.util.PropertiesUtil;


@WebServlet("/CurdServlet")
public class CurdServlet extends HttpServlet {

    public CurdServlet() {
        super();
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String page = req.getParameter("page");
        if (page == null) {
            page = "1";
        }

        PageUtil pageUtil = new PageUtil(Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue("pageSize")));

        String search = req.getParameter("search");
        String add_name = req.getParameter("add_name");
        String add_chef = req.getParameter("add_chef");
        String add_price = req.getParameter("add_price");
        String xiugai_name = req.getParameter("xiugai_name");
        String xiugai_chef = req.getParameter("xiugai_chef");
        String xiugai_price = req.getParameter("xiugai_price");
        String sq_name = req.getParameter("sq_name");

        CurdDaoImpl dao = new CurdDaoImpl();
        JDBCUtil jdbcUtil = new JDBCUtil();
        Connection con = null;

        try {
            con = jdbcUtil.getCon();
            List<Curd> list = new ArrayList<Curd>();

            int totalNum = dao.menuCount(con);

            if (search != null) {
                Curd curd = dao.find(search, con);
                list.add(curd);
                search = "";
            } else {
                list = dao.findAll(con,pageUtil);
            }
            if (add_name != null) {
                Curd curd = new Curd();
                curd.setName(add_name);
                curd.setChef(add_chef);
                curd.setPrice(Integer.parseInt(add_price));
                dao.insert(curd, con);
               list = dao.findAll(con,pageUtil);
                add_name = "";
                add_chef = "";
                add_price = "";
            }
            if (xiugai_name != null) {
                Curd curd = new Curd();
                curd.setName(xiugai_name);
                curd.setChef(xiugai_chef);
                curd.setPrice(Integer.parseInt(xiugai_price));
                dao.update(curd, con);
                list = dao.findAll(con,pageUtil);
                xiugai_name = "";
                xiugai_chef = "";
                xiugai_price = "";
            }
            if (sq_name != null) {
                dao.delete(sq_name, con);
                list = dao.findAll(con,pageUtil);
                sq_name = "";
            }


            System.out.println(totalNum);

            String pageCode = this.genPageCode(totalNum, Integer.parseInt(PropertiesUtil.getValue("pageSize")), Integer.parseInt(page));
            req.setAttribute("pageCode", pageCode);
            req.setAttribute("list", list);
            req.getRequestDispatcher("curd.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                jdbcUtil.closeCon(con);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
    public String genPageCode(int totalNum,int pageSize,int currentPage){
        int totalpage=totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;

        StringBuffer sb = new StringBuffer();
        sb.append("<li><a href='CurdServlet?page=1'>首页</a></li>");

        if(currentPage<=1){
            sb.append("<li class='disabled'><a href='#'>上一页</a></li>");
        }else{
            sb.append("<li><a href='CurdServlet?page="+(currentPage-1)+"'>上一页</a></li>");
        }
        // 最多5页
        for (int i = currentPage-2; i <currentPage+2 ; i++) {
            if(i<1 || i>totalpage){
                continue;
            }
            if(i==currentPage){
                sb.append("<li class='active'><a href='CurdServlet?page="+i+"'>"+i+"</a></li>");
            }else{
                sb.append("<li><a href='CurdServlet?page="+i+"'>"+i+"</a></li>");
            }
        }

        if(currentPage>=totalpage){
            sb.append("<li class='disabled'><a href='#'>下一页</a></li>");
        }else{
            sb.append("<li><a href='CurdServlet?page="+(currentPage+1)+"'>下一页</a></li>");
        }


        sb.append("<li><a href='CurdServlet?page="+totalpage+"'>尾页</a></li>");

        return sb.toString();
    }


    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

}
